/*
 * 版权所有(C)，上海海鼎信息工程股份有限公司，lizhixing，所有权利保留。
 * <p>
 * 项目名：	boot-demo
 * 文件名：	SwaggerConfiguration
 * 模块说明：
 * 修改历史：
 * 2022/2/15 - lizhixing - 创建。
 */
package com.lovely602.boot.bootdemo.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.BasicAuth;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

/**
 * swagger3版本
 * 
 * @author lizhixing
 */
@Configuration
@EnableOpenApi
public class SwaggerConfiguration {

  @Bean
  public Docket swaggerDocket() {
    return new Docket(DocumentationType.SWAGGER_2)//
        .groupName("demo")//
        .apiInfo(buildApiInfo())//
        .select()//
        .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))//
        .build()//
        .globalRequestParameters(this.getParameters())//
        .securitySchemes(securitySchemes())//
        .securityContexts(securityContexts());
  }

  private List<RequestParameter> getParameters() {

    RequestParameterBuilder parameterBuilder = new RequestParameterBuilder();
    RequestParameter parameter = parameterBuilder.name("Authorization").description("令牌")
        .in(ParameterType.HEADER).required(false).build();
    List<RequestParameter> parameters = new ArrayList<>();
    parameters.add(parameter);
    return parameters;
  }

  private ApiInfo buildApiInfo() {
    return new ApiInfoBuilder().title("boot-demo接口文档").version("1.0.0").build();
  }

  private List<SecurityScheme> securitySchemes() {
    List<SecurityScheme> schemes = new ArrayList<>();
    schemes.add(new BasicAuth("basicAuth"));
    return schemes;
  }

  private List<SecurityContext> securityContexts() {
    List<SecurityContext> securityContexts = new ArrayList<>();
    securityContexts.add(SecurityContext.builder().securityReferences(defaultAuth())
        .forPaths(PathSelectors.any()).build());
    return securityContexts;
  }

  private List<SecurityReference> defaultAuth() {
    AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
    AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
    authorizationScopes[0] = authorizationScope;
    List<SecurityReference> securityReferences = new ArrayList<>();
    securityReferences.add(new SecurityReference("basicAuth", authorizationScopes));
    return securityReferences;
  }

}
